<?php

require_once('constants.php');
require_once('db.php');

function showHeader() {
?>    
<table border="0">
	<tbody class="image_desc" style="text-align: center;">
	<tr style="height: 71px;">
		<td style="width: 250px;"></td>
		<td style="width: 810px; text-align: center; margin: 0; padding: 0;" colspan="3">
            <a href="index.php">
                <img alt="Daily Giggle Homepage" src="images/daily-giggle-logo.png" />
            </a>
        </td>
		<td></td>
	</tr>
	<tr style="height: 5px;">
		<td colspan="5"></td>
	</tr>
    </tbody>
</table>
<?php 
}

function displayImagesByPublishDate(DateTime $publishDate, $imageCount) {
?>

<table border="0">
	<tbody class="image_desc" style="text-align: center;">

<?php  
    $row = dbGetImageInfoByPublishDate($publishDate->format('Y-m-d'));
    for ( $i = 0; $i < $imageCount && $row == true; $i++ ) {
?>

	<tr>
		<td style="width: 260px;"></td>
		<td style="border-style: solid; border-width: 1px; text-align: center; width: 640px;
				   font-weight: bold; font-size: 16px; background-color: #e6e6ff; color: #000028; height: 10px;">
			<?php print($publishDate->format('j F, Y')); ?>
		</td>
		<td></td>
	</tr>
	<tr>
		<td></td>
		<td style="border-style: solid; border-width: 1px;">
            <img style="width: 640px" alt="<?php print($row['description']); ?>" src="php/get_image_by_publish_date.php?publish_date=<?php print($publishDate->format('Y-m-d')); ?>">
		</td>
	</tr>
	<tr>
		<td></td>
		<td class="image_desc" style="border-style: solid; border-width: 1px; text-align: center;
							   background-color: #e6e6ff; padding: 5px; color: #000028">
			<?php print($row['description']); ?><br>
			<a href="<?php print($row['source']); ?>"><?php print($row['source']); ?></a>
		</td>
	</tr>
	<tr style="height: 15px;">
		<td colspan="2"></td>
	</tr>

<?php 
        $publishDate = new DateTime(date('Y-m-d', strtotime("-1 day", DateTime_getTimestamp($publishDate))));
        $row = dbGetImageInfoByPublishDate($publishDate->format('Y-m-d'));
    }   //End of the for loop
?>
	<tr>
		<td></td>
		<td class="image_desc" style="text-align: center; padding: 5px;">
<?php    
    //Print out another "next" link if there are more records
    if ( $row == true ){
        print '<a href="display_images_by_publish_dates.php?publish_date=' . $publishDate->format('Y-m-d') 
               . '&image_count=5">More images...</a>';
    }
?>

		</td>
	</tr>
	<tr style="height: 15px;">
		<td colspan="2"></td>
	</tr>
	</tbody>
</table>

<?php    
}

//Temporary replacement since DateTime->getTimestamp() is not supported in versions before 5.3
function DateTime_getTimestamp(&$dt) {
    $dtz_original = $dt->getTimezone();
    $dtz_utc = new DateTimeZone("UTC");
    $dt->setTimezone($dtz_utc);
    $year = intval($dt->format("Y"));
    $month = intval($dt->format("n"));
    $day = intval($dt->format("j"));
    $hour = intval($dt->format("G"));
    $minute = intval($dt->format("i"));
    $second = intval($dt->format("s"));
    $dt->setTimezone($dtz_original);

    return gmmktime($hour,$minute,$second,$month,$day,$year);
}
?>
